home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tcsh / dist / tw.help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-21  |  5.8 KB  |  196 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/tw.help.c,v 3.4 1991/10/20 01:38:14 christos Exp $ */
  2. /* tw.help.c: actually look up and print documentation on a file.
  3.  *          Look down the path for an appropriate file, then print it.
  4.  *          Note that the printing is NOT PAGED.  This is because the
  5.  *          function is NOT meant to look at manual pages, it only does so
  6.  *          if there is no .help file to look in.
  7.  */
  8. /*-
  9.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *    This product includes software developed by the University of
  23.  *    California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  *
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  */
  40. #include "sh.h"
  41.  
  42. RCSID("$Id: tw.help.c,v 3.4 1991/10/20 01:38:14 christos Exp $")
  43.  
  44. #include "tw.h"
  45. #include "tc.h"
  46.  
  47.  
  48. static int f = -1;
  49. static    sigret_t     cleanf        __P((int));
  50. static    Char        *skipslist    __P((Char *));
  51. static    void         nextslist     __P((Char *, Char *));
  52.  
  53. static char *h_ext[] = {
  54.     ".help", ".1", ".8", ".6", NULL
  55. };
  56.  
  57. void
  58. do_help(command)
  59.     Char   *command;
  60. {
  61.     Char    name[FILSIZ + 1];
  62.     Char   *cmd_p, *ep;
  63.     char  **sp;
  64.  
  65.     sigret_t(*orig_intr) ();
  66.     Char    curdir[MAXPATHLEN];    /* Current directory being looked at */
  67.     register Char *hpath;    /* The environment parameter */
  68.     Char    full[MAXPATHLEN];
  69.     char    buf[512];        /* full path name and buffer for read */
  70.     int     len;        /* length of read buffer */
  71.     Char   *thpath;
  72.  
  73.  
  74.     /* copy the string to a safe place */
  75.     copyn(name, command, FILSIZ + 1);
  76.  
  77.     /* trim off the garbage that may be at the end */
  78.     for (cmd_p = name; *cmd_p != '\0'; cmd_p++)
  79.     if (*cmd_p == ' ' || *cmd_p == '\t')
  80.         *cmd_p = '\0';
  81.  
  82.     /* if nothing left, return */
  83.     if (*name == '\0')
  84.     return;
  85.  
  86.     if (adrof1(STRhelpcommand, &aliases)) {    /* if we have an alias */
  87.     jmp_buf osetexit;
  88.  
  89.     getexit(osetexit);    /* make sure to come back here */
  90.     if (setexit() == 0)
  91.         aliasrun(2, STRhelpcommand, name);    /* then use it. */
  92.     resexit(osetexit);    /* and finish up */
  93.     }
  94.     else {            /* else cat something to them */
  95.     /* got is, now "cat" the file based on the path $HPATH */
  96.  
  97.     hpath = str2short(getenv(SEARCHLIST));
  98.     if (hpath == NULL)
  99.         hpath = str2short(DEFAULTLIST);
  100.     thpath = hpath = Strsave(hpath);
  101.  
  102.     while (1) {
  103.         if (!*hpath) {
  104.         xprintf("No help file for %s\n", short2str(name));
  105.         break;
  106.         }
  107.         nextslist(hpath, curdir);
  108.         hpath = skipslist(hpath);
  109.  
  110.         /*
  111.          * now make the full path name - try first /bar/foo.help, then
  112.          * /bar/foo.1, /bar/foo.8, then finally /bar/foo.6.  This is so
  113.          * that you don't spit a binary at the tty when $HPATH == $PATH.
  114.          */
  115.         copyn(full, curdir, sizeof(full) / sizeof(Char));
  116.         catn(full, STRslash, sizeof(full) / sizeof(Char));
  117.         catn(full, name, sizeof(full) / sizeof(Char));
  118.         ep = &full[Strlen(full)];
  119.         for (sp = h_ext; *sp; sp++) {
  120.         *ep = '\0';
  121.         catn(full, str2short(*sp), sizeof(full) / sizeof(Char));
  122.         if ((f = open(short2str(full), O_RDONLY)) != -1)
  123.             break;
  124.         }
  125.         if (f != -1) {
  126.         /* so cat it to the terminal */
  127.         orig_intr = (sigret_t (*)()) sigset(SIGINT, cleanf);
  128.         while (f != -1 && (len = read(f, (char *) buf, 512)) != 0)
  129.             (void) write(SHOUT, (char *) buf, (size_t) len);
  130.         (void) sigset(SIGINT, orig_intr);
  131.         if (f != -1)
  132.             (void) close(f);
  133.         break;
  134.         }
  135.     }
  136.     xfree((ptr_t) thpath);
  137.     }
  138. }
  139.  
  140. static  sigret_t
  141. /*ARGSUSED*/
  142. cleanf(snum)
  143. int snum;
  144. {
  145. #ifdef UNRELSIGS
  146.     if (snum)
  147.     (void) sigset(SIGINT, cleanf);
  148. #endif /* UNRELSIGS */
  149.     if (f != -1)
  150.     (void) close(f);
  151.     f = -1;
  152. #ifndef SIGVOID
  153.     return (snum);
  154. #endif
  155. }
  156.  
  157. /* these next two are stolen from CMU's man(1) command for looking down
  158.  * paths.  they are prety straight forward. */
  159.  
  160. /*
  161.  * nextslist takes a search list and copies the next path in it
  162.  * to np.  A null search list entry is expanded to ".".
  163.  * If there are no entries in the search list, then np will point
  164.  * to a null string.
  165.  */
  166.  
  167. static void
  168. nextslist(sl, np)
  169.     register Char *sl;
  170.     register Char *np;
  171. {
  172.     if (!*sl)
  173.     *np = '\000';
  174.     else if (*sl == ':') {
  175.     *np++ = '.';
  176.     *np = '\000';
  177.     }
  178.     else {
  179.     while (*sl && *sl != ':')
  180.         *np++ = *sl++;
  181.     *np = '\000';
  182.     }
  183. }
  184.  
  185. /*
  186.  * skipslist returns the pointer to the next entry in the search list.
  187.  */
  188.  
  189. static Char *
  190. skipslist(sl)
  191.     register Char *sl;
  192. {
  193.     while (*sl && *sl++ != ':');
  194.     return (sl);
  195. }
  196.